package com.task.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.task.bean.Student;
import com.task.bean.Task;
import com.task.bean.TaskExec;
import com.task.bean.Teacher;
import com.task.mapper.StudentMapper;
import com.task.mapper.TaskExecMapper;
import com.task.mapper.TaskMapper;
import com.task.mapper.TeacherMapper;
import com.task.service.ITaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.task.vo.TaskItemVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuzhe
 * @since 2021-04-10
 */
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements ITaskService {
    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private TaskExecMapper taskExecMapper;


    @Override
    public List<Task> list(int pageNum, int pageSize, int teaId) {
        IPage<Task> userPage = new Page<>(pageNum, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("tea_id", teaId);
//        queryWrapper.eq("status", 1);
        IPage page = taskMapper.selectPage(userPage, queryWrapper);
        return page.getRecords();
    }

    @Override
    public List<Task> queryAllByStuId(int stuId) {
        // stuId查询自己的老师
        List<Teacher> teacherList = studentMapper.queryTeaByStuId(stuId);
        // 老师ID查询所有的任务
//        List<Integer> collect = teacherList.stream().map(teacher -> teacher.getId()).collect(Collectors.toList());
        if (ObjectUtils.isEmpty(teacherList)) {
            return null;
        }
        List<Task> taskList = taskMapper.queryTaskByTeaIds(teacherList);
        return taskList;
    }

    @Override
    public TaskItemVo getTaskItem(int teaId, int taskId) {
        // 1、计算当前老师的学生
        List<Student> studentList = studentMapper.queryStuByTeaId(teaId);
        // 2、当前任务的总人数
        List<Integer> stuLists = studentList.stream().map(item -> item.getId()).collect(Collectors.toList());
        // 3、已经完成的
        List<Integer> stus = taskExecMapper.selectList(new QueryWrapper<TaskExec>().eq("task_id", taskId).in("stu_id",stuLists)).stream().map(item -> item.getStuId()).collect(Collectors.toList());
        TaskItemVo taskItemVo = new TaskItemVo();
        if (stus.isEmpty() || stus == null) {
            taskItemVo.setFinishStuList(null);
            taskItemVo.setFinishNum(0);
            List<Student> studentNo = studentMapper.selectList(null);
            taskItemVo.setNoFinishStuList(studentList);
            taskItemVo.setNoFinishNum(studentList.size());
        } else {
            List<Student> finishStus = studentMapper.selectBatchIds(stus);
            taskItemVo.setFinishStuList(finishStus);
            taskItemVo.setFinishNum(finishStus.size());
            // stus不在stuLists
            studentList.removeAll(finishStus);
            taskItemVo.setNoFinishStuList(studentList);
            taskItemVo.setNoFinishNum(studentList.size());
        }
        return taskItemVo;
    }

}
